<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog ba-operate-dialog-list-add"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="770"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                <span v-if="baTable.form.operate == 'Add'">{{ t('添加') }}</span>
                <span v-if="baTable.form.operate == 'Edit'">{{ t('编辑') }} {{ baTable.form.items!.contract }}({{ baTable.form.items!.batch }})</span>
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                style="width: 850; padding: 15px;"
            >

                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    :model="baTable.form.items"
                    label-position="top"
                    :inline="true"
                    :label-width="baTable.form.labelWidth + 'px'"
                >
                    <FormItem :label="t('分类')" type="remoteSelect" v-model="baTable.form.items!.category_id" :required="true" :input-attr="{ pk: 'id', field: 'name', 'remote-url': '/system/product.Category/index?order_id=' + state.order_id }" />
                    <FormItem :label="t('产品名')" type="remoteSelect" v-model="baTable.form.items!.product_id" :required="true" :input-attr="{ pk: 'id', field: 'full_name_customer', 'remote-url': '/system/product.Product/index?category_id=' + baTable.form.items!.category_id }" />
                    <el-form-item :label="t('销售价格')">
                        <el-input
                            v-model="baTable.form.items!.sale_price"
                            style="width: 354px;"
                            class="input-with-select"
                            type="number"
                            >
                            <template #append>
                                <el-select v-model="baTable.form.items!.sale_currency" placeholder="Select" class="currency-select">
                                    <el-option label="CNY" value="CNY" />
                                    <el-option label="KRW" value="KRW" />
                                    <el-option label="USD" value="USD" />
                                </el-select>
                            </template>
                        </el-input>
                    </el-form-item>
                    <el-form-item :label="t('购买价格')">
                        <el-input
                            v-model="baTable.form.items!.cost_price"
                            style="width: 354px;"
                            class="input-with-select"
                            type="number"
                            >
                            <template #append>
                                <el-select v-model="baTable.form.items!.cost_currency" placeholder="Select" class="currency-select">
                                    <el-option label="CNY" value="CNY" />
                                    <el-option label="KRW" value="KRW" />
                                    <el-option label="USD" value="USD" />
                                </el-select>
                            </template>
                        </el-input>
                    </el-form-item>
                    <FormItem :label="t('数量')" type="number" v-model="baTable.form.items!.quantity" />
                </el-form>
                
                
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('取消') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('保存') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, TabsPaneContext  } from 'element-plus'

import { useFilter } from '/@/stores/filter'

const filter = useFilter();

const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

import { getProductApi, getProductInfoApi } from '/@/api/most/customer/product'

const { t } = useI18n()

const propss = defineProps(['order_id'])


const state = reactive({
    order_id: propss.order_id,
})

const handleClick = (tab: TabsPaneContext, event: Event) => {
    
}


const changeProduct = () => {
    
    let product_ids = baTable.form.items!.product_ids;
    getProductInfoApi({ product_ids: product_ids }).then((e) => {
        baTable.form.items!.product = e.data;
    })
}

const changeType = () => {
    let type = baTable.form.items!.type
    console.log(type)
    if(type[0] == '内销'){
        baTable.form.items!.neixiao = true;
    }else{
        baTable.form.items!.neixiao = false;
    }
}

const changeCustomer = () => {
    let customer_id = baTable.form.items!.customer_id;
    if(customer_id == ''){
        baTable.form.items!.selectProductStatus = true;
        baTable.form.items!.product = [];
    }else{
        baTable.form.items!.selectProductStatus = false;
        getProductApi({ customer_id: customer_id }).then((e) => {  // 获取产品列表。 
            baTable.form.items!.product_list = e.data;
        })
    }
}

const props = {
    expandTrigger: 'hover',
}



const options = [
    {
        value: '出口',
        label: t('出口'),
        children: [{
            value: '买入交易',
            label: t('买入交易'),
        }, {
            value: '佣金交易',
            label: t('佣金交易'),
        }],
    },{
        value: '进口',
        label: t('进口'),
        children: [{
            value: '买入交易',
            label: t('买入交易'),
        }, {
            value: '佣金交易',
            label: t('佣金交易'),
        }],
    },{
        value: '内销',
        label: t('内销'),
        children: [{
            value: '买入交易',
            label: t('买入交易'),
        }, {
            value: '佣金交易',
            label: t('佣金交易'),
        }],
    },
]

</script>

<style scoped lang="scss">
.el-form {
    --el-form-label-font-size: var(--el-font-size-base);
    --el-form-inline-content-width: 354px;
}
.el-form--inline .el-form-item:nth-child(even) {
    margin-right: 0;
}

.el-radio-group{
    width: 354px;
}

:deep(.el-form--inline .el-input) {
    --el-input-width: 354px;
}

:deep(.currency-select .el-input){
    width: 100px!important;
}   

:deep(.el-form--inline .el-input) {
    --el-input-width: 354px;
}
:deep(.el-form--inline .el-radio-group){
    width: 354px;
}
</style>
